
// ArrowsTestView.cpp : implementation of the CArrowsTestView class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "ArrowsTest.h"
#endif

#include "ArrowsTestDoc.h"
#include "ArrowsTestView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CArrowsTestView

IMPLEMENT_DYNCREATE(CArrowsTestView, CView)

BEGIN_MESSAGE_MAP(CArrowsTestView, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
END_MESSAGE_MAP()

// CArrowsTestView construction/destruction

CArrowsTestView::CArrowsTestView()
{
	// TODO: add construction code here

}

CArrowsTestView::~CArrowsTestView()
{
}

BOOL CArrowsTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

// CArrowsTestView drawing

void CArrowsTestView::OnDraw(CDC* pDC)
{
	CArrowsTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: add draw code for native data here
	CPoint PosCenter(200,200);
	CPoint PosList[8] = {CPoint(100,100),CPoint(200,100),CPoint(300,100),CPoint(300,200),CPoint(300,300),CPoint(200,300),CPoint(100,300),CPoint(100,200)};

	for ( int i = 0; i < 8; i++ )
	{
		pDC->MoveTo(PosCenter);
		pDC->LineTo(PosList[i]);
		DrawDirectionArrows(pDC,PosCenter.x,PosCenter.y,PosList[i].x,PosList[i].y,(0,0),20);
	}
}


// CArrowsTestView printing

BOOL CArrowsTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CArrowsTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CArrowsTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}


// CArrowsTestView diagnostics

#ifdef _DEBUG
void CArrowsTestView::AssertValid() const
{
	CView::AssertValid();
}

void CArrowsTestView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CArrowsTestDoc* CArrowsTestView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CArrowsTestDoc)));
	return (CArrowsTestDoc*)m_pDocument;
}
#endif //_DEBUG

void CArrowsTestView::DrawDirectionArrows( CDC* pDC, int x1, int y1, int x2, int y2, CPoint PosCenter, int nLength )
{
	// PosA and PosB is the target position.
	// PosD is the first position and PosC is the PosD next position.
	CPoint PosA(0,0),PosB(0,0),PosC(x2,y2),PosD(x1,y1);

	int m,n;
	m = PosD.y - PosC.y;
	n = PosC.x - PosD.x;

	PosA.x = PosD.x - m ;
	PosA.y = PosD.y - n ;

	PosB.x = PosD.x + m ;
	PosB.y = PosD.y + n ;
		
	// Process the special case.
	if (PosA.x == PosC.x || PosB.y == PosC.y)
	{
		PosA.y < PosC.y ? PosA.y = PosC.y - nLength : PosA.y = PosC.y + nLength;
		PosB.x < PosC.x ? PosB.x = PosC.x - nLength : PosB.x = PosC.x + nLength;
	}
	else if (PosA.y == PosC.y || PosB.x == PosC.x)
	{
		PosA.x < PosC.x ? PosA.x = PosC.x - nLength : PosA.x = PosC.x + nLength;
		PosB.y < PosC.y ? PosB.y = PosC.y - nLength : PosB.y = PosC.y + nLength;
	}
	else
	{
		double dASlope(0.0f), dBSlope(0.0f), dTempValue(0.0f);
		dASlope = ((double)(PosA.y - PosC.y)) / ((double)(PosA.x - PosC.x));
		dBSlope = ((double)(PosB.y - PosC.y)) / ((double)(PosB.x - PosC.x));

		// Calculate the target position.
		CPoint pTarget(0, 0);
		dTempValue = sqrt(pow((double)nLength, 2) / (pow(dASlope, 2) + 1));
		(PosA.x < PosC.x) ? pTarget.x = (LONG)(PosC.x - dTempValue) : pTarget.x = (LONG)(PosC.x + dTempValue);
		pTarget.y = (LONG)(PosC.y - dASlope*(PosC.x - pTarget.x));
		PosA = pTarget;

		pTarget.SetPoint(0, 0);
		dTempValue = sqrt(pow((double)nLength, 2) / (pow(dBSlope, 2) + 1));
		(PosB.x < PosC.x) ? pTarget.x = (LONG)(PosC.x - dTempValue ): pTarget.x = (LONG)(PosC.x + dTempValue);
		pTarget.y = (LONG)(PosC.y - dBSlope*(PosC.x - pTarget.x));
		PosB = pTarget;
	}

	pDC->MoveTo(PosD+PosCenter);
	pDC->MoveTo(PosC+PosCenter);
	pDC->LineTo(PosA+PosCenter);
	pDC->MoveTo(PosB+PosCenter);
	pDC->LineTo(PosC+PosCenter);

}

// CArrowsTestView message handlers
